<html>
<head><title> CS143 Project 1B </title></head>
<body>

<p>type an SQL query in the following box:</p>
<p>
<form method="GET">
<textarea name="query" cols="60" rows="8"></textarea>
<input type="submit" value="submit" />
</form>
</p><small>Note: tables and fields are case sensitive !!</small></p>


<?php

$query = $_GET["query"];

if ( $query == "")
{
	echo "Please enter a query !!!";
	exit;
}


// Open connection to the DB
$db_connection = mysql_connect("localhost", "cs143", "");

mysql_select_db("TEST", $db_connection);
$rs = mysql_query($query, $db_connection);

$nrow = mysql_num_rows($rs) ;
$nfield = mysql_num_fields($rs) ;


// Fetch the result into a table form

print "<h3> Results from MySQL:</h3>";
print "<table border=1 cellspacing=1 cellpadding=2>"; 

/*
print "<tr align=center>";

for ($i = 0; $i < nfield; $i++)
{
	$field = mysql_field_name($rs, $i);
	print $field;
	print "<td><b>$field</b></td>";	
}

print "</tr>";
*/


while ($row = mysql_fetch_row($rs))
{
	print "<tr align=center>";

	foreach($row as $result)
	{
		if ($result == NULL)
			print "<td>N/A</td>";
		else
			print "<td>$result</td>";
		
	}
	print "</tr>";
}

print "</table>";

mysql_close($db_connection);

?>



</body>
</html>

